//
//  CustomTextView.m
//  FreshMember
//
//  Created by qyc on 2019/4/19.
//  Copyright © 2019年 qyc. All rights reserved.
//

#import "CustomTextView.h"
#import "CheckHelper.h"
@interface CustomTextView()

- (void)refreshThisTextView;

@end

@implementation CustomTextView

-(void)initialize {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshThisTextView) name:UITextViewTextDidChangeNotification object:self];
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

// 纯代码加载
- (instancetype)init {
    self = [super init];
    if (self) {
        [self initialize];
    }
    return self;
}

// xib加载
-(void)awakeFromNib {
    [super awakeFromNib];
    if (self) {
        [self initialize];
        if (_textLength == 0) {
            _textLength = 10000;
        }
    }
}

/** 刷新页面 */
- (void)refreshThisTextView {
    UITextRange * selectedRange = self.markedTextRange;
    if(selectedRange == nil || selectedRange.empty){
        NSString *inPutString = [NSString stringWithFormat:@"%@", self.text];
        
        if (self.isHaveEmoji) {
            
        } else if ([CheckHelper stringContainsEmoji:inPutString]) {
            NSInteger largeTo = inPutString.length - 2;
            if (largeTo >= 0) {
                self.text = [inPutString substringToIndex:self.text.length - 2];
//                [YDProgressHUD showMessage:@"不支持表情输入" time:1];
                if (largeTo==1) {
                    self.text = @"";
                }
                return;
            }
            return;
        }
        
        NSInteger currentLength = inPutString.length;
        
        if (currentLength > self.textLength) {
            inPutString=[inPutString substringWithRange:NSMakeRange(0, self.textLength)];
            self.text=inPutString;
        }
    }
    
    // 刷新placeHolder
    if([[self text] length]) {
        [_placeHolderLabel setAlpha:0];
    } else {
        [_placeHolderLabel setAlpha:1];
    }
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
}

/** 获取当前输入长度（汉字-2 英文-1） */
- (NSInteger)getStringLength:(NSString*)strTemp {
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSData* da = [strTemp dataUsingEncoding:enc];
    return [da length];
}

/** 内容长度限制 */
- (void)setTextLength:(NSInteger)textLength {
    if (_textLength != textLength) {
        _textLength = textLength;
    }
}

/** 内容赋值 */
- (void)setText:(NSString *)text {
    [super setText:text];
    [self refreshThisTextView];
}

/** font赋值 */
-(void)setFont:(UIFont *)font {
    [super setFont:font];
    _placeHolderLabel.font = self.font;
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    [_placeHolderLabel sizeToFit];
    _placeHolderLabel.frame = CGRectMake(8, 8, CGRectGetWidth(self.frame)-16, CGRectGetHeight(_placeHolderLabel.frame));
}

-(void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    
    if ( _placeHolderLabel == nil ) {
        _placeHolderLabel = [[UILabel alloc] init];
        _placeHolderLabel.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
        _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
        _placeHolderLabel.numberOfLines = 0;
        _placeHolderLabel.font = self.font;
        _placeHolderLabel.backgroundColor = [UIColor clearColor];
        _placeHolderLabel.textColor =[UIColor colorWithRed:0.85f green:0.85f blue:0.85f alpha:1.00f];
        _placeHolderLabel.alpha = 1;
        [self addSubview:_placeHolderLabel];
    }
    
    _placeHolderLabel.text = self.placeholder;
    [self refreshThisTextView];
}

//When any text changes on textField, the delegate getter is called. At this time we refresh the textView's placeholder
-(id<UITextViewDelegate>)delegate {
    [self refreshThisTextView];
    return [super delegate];
}

@end
